<?php

namespace App\Http\Controllers\Admin;


use App\Models\ProductionType;
use App\Validate\ProductionTypeValidate;


/**
 * 作品类型馆里
 */
class ProductionTypeController extends CommonController
{
    public $productionTypeModelObj = null;
    public $validateObj = null;

    public function __construct(){
        parent::__construct();

        $this->validateObj = new ProductionTypeValidate();
        $this->productionTypeModelObj = new ProductionType();
    }

    /**
     * 作品类型列表
     * @param keyword 关键字搜索
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     * @param start_time 开始时间
     * @param end_time 结束时间
     */
    public function productionTypeList(){
        $keyword = $this->request->input('keyword' , '');
        $start_time = $this->request->input('start_time' , '');
        $end_time = $this->request->input('end_time' , '');
        $limit = $this->request->input('limit' , 10);
        $page = $this->request->input('page' , 1);
        $res = $this->productionTypeModelObj->select(['id','name','intro','create_time'])
        ->where(function($query) use($keyword , $start_time , $end_time){
                if($keyword){
                    $query->where('name' , 'like' , '%'.$keyword.'%');
                }

                if(!empty($start_time) && !empty($end_time)){
                    $query->whereBetween('create_time' , [$start_time , $end_time]);
                }
        })
        ->where('is_del' , 1)
        ->orderByDesc('id')
        ->paginate($limit)
        ->toArray();

        if($res['data']){
            $res = $this->disPageData($res);
             //增加序号
             $res['data'] = $this->addSerialNumber($res['data'], $page , $limit);
            
            return $this->returnApi(200 , '获取成功' , true , $res);
        }

        return $this->returnApi(203 , '暂无数据');
    }

    
    /**
     * 作品类型列表（用于筛选）
     * @param keyword 关键字搜索
     */
    public function productionTypeFilterList()
    {
        $keyword = $this->request->input('keyword', '');
        $res = $this->productionTypeModelObj->select(['id', 'name'])
            ->where(function ($query) use ($keyword) {
                if ($keyword) {
                    $query->where('name', 'like', '%' . $keyword . '%');
                }
            })
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->get();

        if ($res) {
            return $this->returnApi(200, '获取成功', true, $res);
        }

        return $this->returnApi(203, '暂无数据');
    }

    /**
     * 作品类型详情
     * @param id 作者id
     */
    public function productionTypeInfo()
    {
        //增加验证场景进行验证
        if (!$this->validateObj->scene('production_type_info')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validateObj->getError());
        }
        $res = $this->productionTypeModelObj->select(['id','name','intro','create_time'])->where('is_del', 1)->find($this->request->id);
        if (empty($res)) {
            return $this->returnApi(202, '获取失败');
        }
        return $this->returnApi(200, '获取成功', true, $res);
    }

    /**
     * 作品类型添加
     * @param name 作品类型名称    必选
     * @param intro 作品类型简介
     */

     public function productionTypeAdd(){
        //增加验证场景进行验证
        if (!$this->validateObj->scene('production_type_add')->check($this->request->all())) {
            return $this->returnApi(201 ,  $this->validateObj->getError());
        }

        $is_exists = $this->productionTypeModelObj->productionTypeNameIsExists($this->request->name);
        if($is_exists){
            return $this->returnApi(202 , '此名称已存在，请重新添加'); 
        }

        $res = $this->productionTypeModelObj->add($this->request);


        if($res){
            return $this->returnApi(200 , '添加成功' , true);
        }

        return $this->returnApi(202 , '添加失败');
     }

     /**
     * 作品类型修改
     * @param id  作品类型id  必选
     * @param name 作品类型名称    必选
     * @param intro 作品类型简介
     */

    public function productionTypeChange(){
        //增加验证场景进行验证
        if (!$this->validateObj->scene('production_type_change')->check($this->request->all())) {
            return $this->returnApi(201 ,  $this->validateObj->getError());
        }
        $is_exists = $this->productionTypeModelObj->productionTypeNameIsExists($this->request->name ,$this->request->id);
        if($is_exists){
            return $this->returnApi(202 , '此名称已存在，请重新修改'); 
        }

        $res = $this->productionTypeModelObj->change($this->request);

        if($res){
            return $this->returnApi(200 , '修改成功' , true);
        }

        return $this->returnApi(202, '修改失败');
     }

    /**
     * 作品类型删除
     * @param id  作品类型id  必选
     */

    public function productionTypeDel(){
        //增加验证场景进行验证
        if (!$this->validateObj->scene('production_type_del')->check($this->request->all())) {
            return $this->returnApi(201 ,  $this->validateObj->getError());
        }
        $res = $this->productionTypeModelObj->where('is_del' , 1)->find($this->request->id);
        if(empty($res)){
            return $this->returnApi(202 , '删除失败');
        }
        $res->is_del = 2;
        $result = $res->save();

        if($result){
            return $this->returnApi(200 , '删除成功' , true);
        }

        return $this->returnApi(202 , '删除失败');
     }

}
